﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.ConnectionUI;

namespace Liberate.Samples.Database
{
    public partial class TableSelector : Form
    {
        public string ConnectionString { get; set; }
        public DataProvider DbProvider { get; set; }

        public List<String> SelectedTables { get; private set; }

        public TableSelector()
        {
            InitializeComponent();
            SelectedTables = new List<string>();
            DbProvider = DataProvider.SqlDataProvider;
        }

        protected override void OnLoad(EventArgs e)
        {
            BindTables();
            base.OnLoad(e);
        }

        private void BindTables()
        {
            if (string.IsNullOrEmpty(ConnectionString)) return;

            if(DbProvider == DataProvider.SqlDataProvider)
            {
                var sql = "SELECT [name] FROM sysobjects where xtype='u'";
                try
                {
                    using (IDbConnection connection = new SqlConnection(ConnectionString))
                    {
                        IDbCommand command = connection.CreateCommand();
                        command.CommandType = CommandType.Text;
                        command.CommandText = sql;
                        connection.Open();
                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            checkedListBox1.Items.Add(reader[0] as string);
                        }
                        reader.Close();
                        connection.Close();
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
        }

        private void llSetConnection_Click(object sender, EventArgs e)
        {
            try
            {
                var dialog = new DataConnectionDialog();
                DataSource.AddStandardDataSources(dialog);
                dialog.SaveSelection = true;
                dialog.SelectedDataSource = DataSource.SqlDataSource;
                dialog.SelectedDataProvider = DataProvider.SqlDataProvider;
                DialogResult dr = DataConnectionDialog.Show(dialog);
                if (dr == DialogResult.OK)
                {
                    ConnectionString = dialog.ConnectionString;
                    DbProvider = dialog.SelectedDataProvider;
                    BindTables();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            SelectedTables.Clear();
            foreach (string s in checkedListBox1.CheckedItems)
            {
                SelectedTables.Add(s);
            }
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}
